package org.abettor.angleworm.media.conf;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;

import org.abettor.util.ClassPathLoader;

/**
 * <p>应用程序运行环境初始化工具类</p>
 * <p><i>此类不应引用JavaSE之外的任何API</i></p>
 * @author abettor
 *
 */
public abstract class ApplicationEnvironment
{
	/**
	 * 加载必要的classpath
	 * @throws IOException
	 * @throws SecurityException
	 * @throws IllegalArgumentException
	 * @throws NoSuchMethodException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws ClassNotFoundException 
	 */
	@SuppressWarnings("deprecation")
	public static void loadClassPaths() throws IOException, SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, ClassNotFoundException
	{
		//	获取当前应用程序的安装目录
		File f = new File(".");
		String appPath = f.getAbsolutePath();
		if(appPath.endsWith(File.separator + "."))
		{
			appPath = appPath.substring(0, appPath.length() - 2);
		}
		if(appPath.endsWith(File.separator + "bin"))
		{
			appPath = appPath.substring(0, appPath.length() - 4);
		}

		//	加载本应用程序lib中的jars
		File lib = new File(appPath + File.separator + "lib");
		File[] jars = lib.listFiles(new FilenameFilter()
		{
			@Override
			public boolean accept(File dir, String name)
			{
				if(name.toLowerCase().endsWith(".jar"))
				{
					return true;
				}

				return false;
			}
		});
		for(File jar : jars)
		{
			ClassPathLoader.addFile(jar);
		}

		//	加载配置文件，不存在就创建一个
		boolean persist = false;
		String conf = appPath + File.separator + "conf" + File.separator + "global.properties";
		File file = new File(conf);
		if(! file.exists())
		{
			file.createNewFile();
			persist = true;
		}

		FileInputStream fis = new FileInputStream(file);
		Properties prop = new Properties();
		prop.load(fis);
		fis.close();

		//	读取jmf的安装目录
		String jmfHome = prop.getProperty("JMFHOME");
		if(jmfHome == null || jmfHome.isEmpty())
		{
			jmfHome = System.getProperty("JMFHOME");
			if(jmfHome == null || jmfHome.isEmpty())
			{
				System.out.println("Please enter the path of JMFHOME:");
				BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
				jmfHome = br.readLine();
				System.setProperty("JMFHOME", jmfHome);
			}

			prop.setProperty("JMFHOME", jmfHome);
			persist = true;
		}

		//	加载jmf的jars
		File jmfLib = new File(jmfHome + File.separator + "lib");
		jars = jmfLib.listFiles(new FilenameFilter()
		{
			@Override
			public boolean accept(File dir, String name)
			{
				if(name.toLowerCase().endsWith(".jar"))
				{
					return true;
				}

				return false;
			}
		});
		for(File jar : jars)
		{
			ClassPathLoader.addFile(jar);
		}

		//	注册MediaTransmitter的工厂类
		String factoryClass = prop.getProperty("TRANSMITTER_FACTORY");
		if(factoryClass == null || factoryClass.isEmpty())
		{
			System.out.println("Please enter the concrete class name of MediaTransmitterFactory:");
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			factoryClass = br.readLine();
			prop.setProperty("TRANSMITTER_FACTORY", factoryClass);
			persist = true;
		}
		Class.forName(factoryClass);

		//	持久化配置信息
		if(persist)
		{
			FileOutputStream fos = new FileOutputStream(file);
			prop.save(fos, null);
			fos.flush();
			fos.close();
		}
	}
}
